<template>
  <div>
    <h2>添 加 病 害</h2>
    <div class="add">
      <div class="left">
        <el-form :label-position="labelPosition" label-width="80px">
          <el-form-item label="名称">
            <el-input v-model="diseaseName"></el-input>
          </el-form-item>
          <el-form-item label="发病症状">
            <el-input v-model="diseaseSymptoms"></el-input>
          </el-form-item>

          <el-form-item label="图片">
            <el-upload
              class="avatar-uploader"
              action="/api/pest/upload2"
              :http-request="testUpload"
              :show-file-list="false"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="主要危害">
            <el-input v-model="diseaseMainHarm"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <div>
        <el-form :label-position="labelPosition" label-width="80px">
          <el-form-item label="病原">
            <el-input v-model="diseaseSource"></el-input>
          </el-form-item>
          <el-form-item label="发病规律">
            <el-input v-model="diseaseRegularity"></el-input>
          </el-form-item>
          <el-form-item label="防治措施">
            <el-input v-model="diseaseControl"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-row>
      <el-button plain @click="addDisease()">添加</el-button>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      diseaseName: "",
      diseaseSource: "",
      diseaseSymptoms: "",
      diseaseRegularity: "",
      diseasePic: "",
      diseaseControl: "",
      diseaseMainHarm: "",
      labelPosition: "left",
      imageUrl: "",
    };
  },
  methods: {
    async testUpload(item) {
      this.diseasePic = await this.ajax.uploadSubmit("/api/pest/upload2", {
        file: item.file,
      });
      //console.log(this.pestLarvaPic);
      this.imageUrl = "/api/image/" + this.diseasePic;
      this.diseasePic = "/image/" + this.diseasePic;
      console.log(this.diseasePic);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    async addDisease() {
      let paramObj = {
        diseaseName: this.diseaseName,
        diseaseSource: this.diseaseSource,
        diseaseSymptoms: this.diseaseSymptoms,
        diseaseRegularity: this.diseaseRegularity,
        diseasePic: this.diseasePic,
        diseaseControl: this.diseaseControl,
        diseaseMainHarm: this.diseaseMainHarm,
      };
      let info = await this.ajax.getSubmit("/api/disease/add", paramObj);
      console.log(info);

      this.diseaseName = "";
      this.diseaseSource = "";
      this.diseaseSymptoms = "";
      this.diseaseRegularity = "";
      this.diseasePic = "";
      this.diseaseControl = "";
      this.diseaseMainHarm = "";
    },
  },
};
</script>

<style>
</style>